{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import trimesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# load a file by name or from a buffer\n",
    "mesh = trimesh.load_mesh('../models/featuretype.STL')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# is the current mesh watertight?\n",
    "mesh.is_watertight"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-16"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# what's the euler number for the mesh?\n",
    "mesh.euler_number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.77924077444669315"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# the convex hull is another Trimesh object that is available as a property\n",
    "# lets compare the volume of our mesh with the volume of its convex hull\n",
    "np.divide(mesh.volume, mesh.convex_hull.volume)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# since the mesh is watertight, it means there is a\n",
    "# volumetric center of mass which we can set as the origin for our mesh\n",
    "mesh.vertices -= mesh.center_mass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  6.93059627e+00,   1.43877613e-03,   1.49424850e-01],\n",
       "       [  1.43877613e-03,   2.19191960e+01,   1.25194047e-04],\n",
       "       [  1.49424850e-01,   1.25194047e-04,   2.62344872e+01]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# what's the moment of inertia for the mesh?\n",
    "mesh.moment_inertia"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([<trimesh.base.Trimesh object at 0x7f4ac82adc18>], dtype=object)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# if there are multiple bodies in the mesh we can split the mesh by\n",
    "# connected components of face adjacency\n",
    "# since this example mesh is a single watertight body we get a list of one mesh\n",
    "mesh.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# facets are groups of coplanar adjacent faces\n",
    "# set each facet to a random color\n",
    "# colors are 8 bit RGBA by default (n,4) np.uint8\n",
    "for facet in mesh.facets:\n",
    "    mesh.visual.face_colors[facet] = trimesh.visual.random_color()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<trimesh.scene.scene.Scene at 0x7f4ab8144eb8>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# preview mesh in an opengl window if you installed pyglet with pip\n",
    "mesh.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<trimesh.base.Trimesh at 0x7f4ac82d2908>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# transform method can be passed a (4,4) matrix and will cleanly apply the transform\n",
    "mesh.apply_transform(trimesh.transformations.random_rotation_matrix())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TrackedArray([ 3.96411557,  4.38672234,  4.89730488])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# an axis aligned bounding box is available\n",
    "mesh.bounding_box.primitive.extents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TrackedArray([ 2.5  ,  5.   ,  1.375])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# a minimum volume oriented bounding box is available\n",
    "mesh.bounding_box_oriented.primitive.extents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TrackedArray([[ 0.13862133, -0.56017358, -0.81669437,  0.12112506],\n",
       "       [ 0.9009058 ,  0.41380035, -0.1309122 ,  0.01552048],\n",
       "       [ 0.41128197, -0.71761747,  0.56202518, -0.07467186],\n",
       "       [ 0.        ,  0.        ,  0.        ,  1.        ]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mesh.bounding_box_oriented.primitive.transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<trimesh.scene.scene.Scene at 0x7f4ab80ec908>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# show the mesh overlayed with its oriented bounding box\n",
    "# the bounding box is a trimesh.primitives.Box object, which subclasses\n",
    "# Trimesh and lazily evaluates to fill in vertices and faces when requested\n",
    "# (press w in viewer to see triangles)\n",
    "(mesh + mesh.bounding_box_oriented).show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17.18750000000001 40.7875061606 95.8943899752\n"
     ]
    }
   ],
   "source": [
    "# bounding spheres and bounding cylinders of meshes are also\n",
    "# available, and will be the minimum volume version of each\n",
    "# except in certain degenerate cases, where they will be no worse\n",
    "# than a least squares fit version of the primitive.\n",
    "print(mesh.bounding_box_oriented.volume, \n",
    "      mesh.bounding_cylinder.volume,\n",
    "      mesh.bounding_sphere.volume)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
